//go:build linux

package gateway_mac

import (
	"fmt"
	"os/exec"
	"strings"
)

func discoverGatewayMacOSSpecific() (mac string, err error) {
	routeCmd := exec.Command("arp", "-a")
	output, err := routeCmd.CombinedOutput()
	if err != nil {
		return "", err
	}
	mac_ip, err := DiscoverGateway()
	if err != nil {
		return "", err
	}

	arpTable := make(map[string]string)
	for index, line := range strings.Split(string(output), "\n") {
		m := strings.Fields(line)
		fmt.Println(index, len(m), m)
		if len(m) != 7 {
			continue
		}
		arpTable[m[1]] = m[3]
	}

	if len(arpTable) == 0 {
		return "", fmt.Errorf("no arp table found")
	}

	//format is: ? (192.168.1.1) at e8:9c:25:75:04:22 [ether] on ens33
	return arpTable["("+mac_ip.String()+")"], nil
}
